﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kevin.Shop.Business.SalesManagement.Components;
using Kevin.Shop.Business.Service.Entity.Helper;
using RiderShop.Mobile.Common.Component;
using Kevin.Shop.Business.StoreManagement.Components.Store;
using Kevin.Comm.Utility;
using RiderShop.Mobile.Common.Helper;
using RiderShop.Mobile.ServiceEntity.MyAccount;

namespace RiderShop.Mobile.Modle.MyAccount
{
    public static partial class MyAccountModel
    {
        /// <summary>
        /// 得到全部省份根据parentId
        /// </summary>
        /// <param name="userId">parentId</param>
        /// <returns></returns>
        public static List<RegionBasicInfo> GetRegionListByParentId(int parentId)
        {
            List<RegionBasicInfo> regionList = RestfulHelper.Query<RegionBasicInfo>(SiteUrl.FormatUrl("SalesManagement_GetRegionBasicInfo", parentId));

            return regionList;
        }

        public static string GetRegionByRegionId(int regionId)
        {
            List<RegionInfo> list = RestfulHelper.Query<RegionInfo>(SiteUrl.FormatUrl("Common_GetRegionById", regionId));
            if (list.Count > 0)
                return list[0].Path;
            return string.Empty;
        }

        public static UserShipingAddressExtend GetShippingAddressWithRegionList(int shippingId)
        {
            UserShipingAddressExtend model = null;
            if (shippingId > 0)
            {
                List<UserShipingAddressExtend> shippingAddress = GetShippingAddress(WebClientContext.Current.UserId, shippingId);

                if (shippingAddress.Count > 0)
                {
                    model = shippingAddress[0];
                }
            }
            var regions = LoadAllRegions();


            if (model == null)
            {
                model = new UserShipingAddressExtend();
            }

            model.ProvinceList = new List<RegionBasicInfo>() { new RegionBasicInfo() { RegionId = 0, RegionName = "省份" } };
            model.CityList = new List<RegionBasicInfo>() { new RegionBasicInfo() { RegionId = 0, RegionName = "城市" } };
            model.RegionList = new List<RegionBasicInfo>() { new RegionBasicInfo() { RegionId = 0, RegionName = "区县" } };

            if (model.ShippingId <= 0)
            {
                regions.ForEach(r =>
                {
                    if (r.ParentId == 0)
                    {
                        model.ProvinceList.Add(new RegionBasicInfo()
                        {
                            RegionId = r.RegionId,
                            RegionName = r.RegionName
                        });
                    }
                });
            }
            else
            {
                var region = GetRegion(regions, model.RegionId);
                if (region != null)
                {
                    regions.ForEach(r =>
                    {
                        if (r.ParentId == region.ParentId)
                        {
                            model.RegionList.Add(new RegionBasicInfo()
                            {
                                RegionId = r.RegionId,
                                RegionName = r.RegionName
                            });
                        }
                    });

                    var city = GetRegion(regions, region.ParentId);
                    if (city != null)
                    {
                        model.CityId = city.RegionId;
                        regions.ForEach(r =>
                        {
                            if (r.ParentId == city.ParentId)
                            {
                                model.CityList.Add(new RegionBasicInfo()
                                {
                                    RegionId = r.RegionId,
                                    RegionName = r.RegionName
                                });
                            }
                        });
                        var province = GetRegion(regions, city.ParentId);
                        if (province != null)
                        {
                            model.ProvinceId = province.RegionId;
                            regions.ForEach(r =>
                            {
                                if (r.ParentId == province.ParentId)
                                {
                                    model.ProvinceList.Add(new RegionBasicInfo()
                                    {
                                        RegionId = r.RegionId,
                                        RegionName = r.RegionName
                                    });
                                }
                            });
                        }
                    }
                }
            }
            return model;
        }


        public static string GetRegionFullName(int regionId)
        {
            var fullName = string.Empty;
            var regions = LoadAllRegions();

            Region district = GetRegion(regions, regionId);
            if (district != null)
            {
                fullName = district.RegionName;
                Region city = GetRegion(regions, district.ParentId);
                if (city != null)
                {
                    fullName = city.RegionName + " " + fullName;
                    Region province = GetRegion(regions, city.ParentId);
                    if (province != null)
                    {
                        fullName = province.RegionName + " " + fullName;
                    }
                }
            }
            return fullName;
        }

        public static Region GetRegion(List<Region> regions, int regionID)
        {
            var result = from m in regions
                         where m.RegionId == regionID
                         select m;

            if (result.Count() > 0)
            {
                Region region = result.Single();
                return region;
            }
            else
            {
                return null;
            }
        }

        public static List<Region> LoadAllRegions()
        {
            List<Region> list = null;
            if (!KevinCache.Exists(CacheKeys.CacheKeyAllRegion))
            {
                list = RestfulHelper.Query<Region>(SiteUrl.FormatUrl("StoreManagement_GetRegions"));
                KevinCache.Insert(CacheKeys.CacheKeyAllRegion, list, 60 * 60);
            }
            else
            {
                list = KevinCache.Get(CacheKeys.CacheKeyAllRegion) as List<Region>;
            }
            return list;
        }

    }
}
